package cn.iocoder.yudao.module.reservation.service.services;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.reservation.controller.admin.services.vo.ServicesPageReqVO;
import cn.iocoder.yudao.module.reservation.controller.admin.services.vo.ServicesSaveReqVO;
import cn.iocoder.yudao.module.reservation.dal.dataobject.services.ServicesDO;
import cn.iocoder.yudao.module.reservation.dal.mysql.services.ServicesMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.SERVICES_NOT_EXISTS;

/**
 * 店铺服务 Service 实现类
 *
 * @author yy
 */
@Service
@Validated
public class ServicesServiceImpl implements ServicesService {

    @Resource
    private ServicesMapper servicesMapper;

    @Override
    public Long createServices(ServicesSaveReqVO createReqVO) {
        // 插入
        ServicesDO services = BeanUtils.toBean(createReqVO, ServicesDO.class);
        servicesMapper.insert(services);
        // 返回
        return services.getId();
    }

    @Override
    public void updateServices(ServicesSaveReqVO updateReqVO) {
        // 校验存在
        validateServicesExists(updateReqVO.getId());
        // 更新
        ServicesDO updateObj = BeanUtils.toBean(updateReqVO, ServicesDO.class);
        servicesMapper.updateById(updateObj);
    }

    @Override
    public void deleteServices(Long id) {
        // 校验存在
        validateServicesExists(id);
        // 删除
        servicesMapper.deleteById(id);
    }

    private void validateServicesExists(Long id) {
        if (servicesMapper.selectById(id) == null) {
            throw exception(SERVICES_NOT_EXISTS);
        }
    }

    @Override
    public ServicesDO getServices(Long id) {
        return servicesMapper.selectById(id);
    }

    @Override
    public PageResult<ServicesDO> getServicesPage(ServicesPageReqVO pageReqVO) {
        return servicesMapper.selectPage(pageReqVO);
    }

}